/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package provider;

import Converters.NettyConverter;
import core.NetPaquet;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMessage;

/**
 *
 * @author MOI
 */
public class ProviderBootstrap {


    static public ClientBootstrap creerProvider(NettyConverter proxy)
    {

        //Création d'un client
       ClientBootstrap cb = new ClientBootstrap(
                  new NioClientSocketChannelFactory(
                          Executors.newCachedThreadPool(),
                          Executors.newCachedThreadPool()));
       
       cb.setPipelineFactory(new ProviderPipelineFactory(proxy));
      return cb;
    }

    static public void send(ClientBootstrap cb,NetPaquet paquet)
    {
         String remoteHost = HttpHeaders.getHost((HttpMessage)paquet.getData());
       int remotePort=80;
      ChannelFuture f = cb.connect(new InetSocketAddress(remoteHost, remotePort));
        System.out.println("Provider créé : "+remoteHost+":"+remotePort);
      Channel channel = f.awaitUninterruptibly().getChannel();
        if(f.isSuccess())
        {
            channel.write(paquet.getData());
            System.out.println("Data send...");
        }else{
            System.err.println("Provider ne peut pas se connecter sniff...");
        }
    }
}
